// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using NUnit.Framework;
using ValidationAspects.Exceptions;

namespace TestValidationAspects.Exceptions
{
	[TestFixture]
	public class TestValidateMatchesRegexException
	{
		[SetUp]
		public void SetUp()
		{
			ValidateMatchesRegexException.ResetMessageFormat();
		}

		[Test]
		public void ValidateRegexException_Constructor()
		{
			ValidateMatchesRegexException exception = new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC");
			Assert.AreEqual("[0-9]{1,10}", exception.Pattern);
			Assert.AreEqual("ABC", exception.Value);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void ValidateRegexException_Constructor_Throws_For_Null_Pattern()
		{
			new ValidateMatchesRegexException(null, "!");
		}

		[Test]
		public void ValidateRegexException_Default_MessageFormat_for_null_Name()
		{
			ValidateMatchesRegexException exception = new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC") {Name = null};
			Assert.AreEqual(string.Format("{0} does not match Regex[{1}].", exception.GetValidationTargetName(), @"[0-9]{1,10}"), ValidateMatchesRegexException.MessageFormat(exception));
		}

		[Test]
		public void ValidateRegexException_Default_MessageFormat_for_empty_Name()
		{
			ValidateMatchesRegexException exception = new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC") {Name = string.Empty};
			Assert.AreEqual(string.Format("{0} does not match Regex[{1}].", exception.GetValidationTargetName(), @"[0-9]{1,10}"), ValidateMatchesRegexException.MessageFormat(exception));
		}

		[Test]
		public void ValidateRegexException_Default_MessageFormat_non_null_or_empty_Name()
		{
			ValidateMatchesRegexException exception = new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC") {Name = "Customer Id"};
			Assert.AreEqual(string.Format("{0} is not a valid {1}.", exception.GetValidationTargetName(), "Customer Id"), ValidateMatchesRegexException.MessageFormat(exception));
		}

		[Test]
		public void ValidateRegexException_Get_Message_Executes_MessageFormat()
		{
			const string message = "custom message";
			ValidateMatchesRegexException.MessageFormat = e => message;
			Assert.AreEqual(message, new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC").Message);
		}

		[Test]
		public void ValidateRegexException_Can_ResetMessageFormat()
		{
			const string message = "custom message";
			ValidateMatchesRegexException.MessageFormat = e => message;

			ValidateMatchesRegexException exception = new ValidateMatchesRegexException(@"[0-9]{1,10}", "ABC");
			Assert.AreEqual(message, exception.Message);

			ValidateMatchesRegexException.ResetMessageFormat();
			Assert.AreEqual(string.Format("{0} does not match Regex[{1}].", exception.GetValidationTargetName(), @"[0-9]{1,10}"), ValidateMatchesRegexException.MessageFormat(exception));
		}
	}
}